// pages/project/project.js
var app=getApp();
var offset= 20;
var current_page=0;
Page({

  /**
   * 页面的初始数据
   */
  data: {

    /**正在搜索标志位 */
    isSearching:false,
    addflag: true, //判断是否显示搜索框右侧部分
    addimg: '../../images/activity_add.png',
    searchstr: '',
    /**项目信息列表数据 */
    projectlist: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.request({
      url: app.url.head_url + '/api/GetAllProjectByOffset?start=0&offset=' + offset,
      headers: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        //console.log(res.data)
        that.setData({
          projectlist: res.data
        })
      },
      fail: function(err) {
        console.log(err);
      },
      complete: function() {

      }
    })
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("下拉刷新");
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var that = this;
    wx.request({
      url: app.url.head_url + '/api/GetAllProjectByOffset?start=0&offset=' + offset,
      header: {
        'content-type': 'application/text'
      },
      success: function (res) {
        that.setData({
          projectlist: res.data
        });
        console.log(res.data);
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
      },
      fail: function (err) {
        console.log(err);
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
      },
      complete: function () {
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
      }
    })
  },


  onReachBottom:function(){
    var that = this;
    current_page=current_page+offset;
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    wx.request({
      url: app.url.head_url + '/api/GetAllProjectByOffset?start='+current_page+'&offset=' + offset,
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        // console.log(res.data)
        var project_list = that.data.projectlist;
        const oldData = that.data.projectlist;
        that.setData({
          projectlist: oldData.concat(res.data)
        })
        // 隐藏加载框
        wx.hideLoading();
      },
      fail: function (err) {
        console.log(err);
        current_page=current_page-offset;
        // 隐藏加载框
        wx.hideLoading();
      },
      complete: function () {
        // 隐藏加载框
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
 

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  goDetailPage:function(e){
    let id=e.currentTarget.dataset.id;
    console.log("111"+id);
    let str = JSON.stringify(id);
    wx.navigateTo({
      url: '../detail/detail?id='+str
    })
  },

  tap(e) {

  },

  // 搜索框右侧 事件
  addhandle() {
    console.log('触发搜索框右侧事件')
  },

  //搜索框输入时触发
  searchList(ev) {
    let e = ev.detail;
    this.setData({
      isSearching:true,
      searchstr: e.detail.value
    })
    console.log("e.detail.value:" + e.detail.value);
    var that = this;
    wx.request({
      url: app.url.head_url + '/api/SearchProject',
      method: "POST",
      data: { "index": e.detail.value},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      dataType: "json",
      success: function (res) {
        console.log(res.data)
        that.setData({
          projectlist: res.data
        })
      },
      fail: function (err) {
        console.log(err);
      },
      complete: function () {

      }
    })
  },


  //搜索回调
  endsearchList(e) {
    console.log('查询数据')
  },


  // 取消搜索
  cancelsearch() {
    this.setData({
      searchstr: '',
      isSearching: false,
    })
  },
  //清空搜索框
  activity_clear(e) {

    this.setData({
      searchstr: ''
    })
  },
})